<?php

class MemberController extends BaseController
{

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * 添加会员
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new Member;

		 //ajax验证
		 $this->performAjaxValidation($model);

		if(isset($_POST['Member']))
		{
			$model->attributes=$_POST['Member'];
			$model->zm_code = $model->createCode();
			$model->zm_psword = $model->passwordEncode($model->zm_psword,1);//登录密码
			$model->zm_paypsword = $model->passwordEncode($model->zm_psword,2);
			$model->inputtime = time();
			if($model->save()){
				$mempro = new MemPro();
				$mempro->attributes = $_POST['MemPro'];
				$mempro->zm_id = $model->zm_id;
				$mempro->save();
			    //成功跳转  
				$this->redirect(array('admin'));
			}
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * 编辑会员
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

			if(isset($_POST['Member']))
		{
			$model->attributes=$_POST['Member'];
			if($model->save()){
				//更新会员附表
				$mempro = MemPro::model()->findByPk((int)$id);
				if(empty($mempro)){
					$mempro = new MemPro();
					$mempro->zm_id = $model->zm_id;
				}
				$mempro->attributes = $_POST['MemPro'];
				$mempro->save();
				//更新会员等级记录表
				$memtype = $_POST['MemType'];
				foreach($memtype as $key=>$val){
					//查找当前会员对应会员组的会员等级记录
					$criteria=new CDbCriteria;
					$criteria->compare('userid',(int)$id);
					$criteria->join .= ' LEFT JOIN {{mem_type}} memtype ON t.itemname=memtype.zmt_id ';
					$criteria->addCondition(' memtype.zmg_id ='.$key);
					$mtype = MemGlog::model()->find($criteria);
					//更新会员等级记录
					MemGlog::model()->updateByPk($mtype->zmo_id,array('itemname'=>$val));
				}
				
			    //成功跳转  
				$this->redirect(array('admin')); 
			}
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}
	
	/**
	 * 重置会员密码
	 */
	public function actionResetPassword($id){
		$model=$this->loadModel($id);
		$hashPass =  rand(100000,999999);
		$model->zm_psword = $model->passwordEncode($hashPass,1);//登录密码
		if($model->Save()){
			//发送邮件组用户
			$message = "你的用户已被管理员重置密码！<br>新密码是：".$hashPass."<br>请及时修改";
			EmailTemplates::model()->sentMail($id,9,array('message'=>$message));
		    //成功跳转  
			 $this->redirect(array('admin'));
		}else{
			//失败跳转
			$this->render('../index/error',array('message'=>'对不起，会员密码重置失败，请稍后再试！'));
			exit;
		}
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('Member');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		//$member =Member::model()->findByPk(50);
		//print_r($member->level_log[0]->level->zmt_title);exit;
		$model=new Member('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Member']))
			$model->attributes=$_GET['Member'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Member::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='member-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}

}
